/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.commands;

import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;

import edu.pku.sei.modeler.exceptions.FactoryNotFoundException;
import edu.pku.sei.modeler.model.common.ModelRegistry;
import edu.pku.sei.modeler.model.structure.MetaBendPoint;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.StructureFactory;

/**
 * @author zl
 *
 */
public class MoveBendpointCommand extends Command {
	private MetaLink link;
	private int index;
	private MetaBendPoint oldBendpoint, newBendpoint;
		
	public MoveBendpointCommand(MetaLink link, Point location, int index) {
		super("Move Bendpoint");
		this.link = link;
		this.index = index;
		StructureFactory sFactory;
		try {
			sFactory = (StructureFactory)ModelRegistry.getRegistry().getStructureFactory(link.getModelElement().getModel());
			newBendpoint = sFactory.createMetaBendPoint();
			newBendpoint.x = location.x;
			newBendpoint.y = location.y;
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FactoryNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		oldBendpoint = (MetaBendPoint)link.getBendPoints().get(index);
	}

	public boolean canExecute() {
		return link != null && index >= 0 && newBendpoint != null && oldBendpoint != null;
	}
		
	public void execute() {
		link.getBendPoints().set(index, newBendpoint);
	}

	public void undo() {
		link.getBendPoints().set(index, oldBendpoint);
	}
}
